# !/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2021/5/7 16:55
# @Author  : qiaoc
# @File    : StampRule.py
# @Desc    : 印章识别稽核


from affix import AffixUtils
from ocr.Ocr import *

"""
    缴费单 - 印章识别稽核
"""
def audit_rule_ele_stamp(all_ocr_attachment,rar_file,auditResult,audit_list,bill_number,
                         file_flag1,affix_list_from_db,file_list1,stamp1,file_flag2,
                         file_list2,stamp2,file_flag3,file_list3,stamp3):
    stamp_result_2 = False
    stamp_result_3 = False
    stamp_count_1, stamp_count_2, stamp_count_3 = 0, 0, 0  # 记录各附件调用次数
    all_ocr_attachment_list = []
    temp_list = []
    for stamp_img in all_ocr_attachment:
        if '附件4' in stamp_img[0] or '附件5' in stamp_img[0] or '附件6' in stamp_img[0]:
            continue
        if stamp_img[1] not in temp_list:
            temp_list.append(stamp_img[1])
            all_ocr_attachment_list.append(stamp_img)

    all_ocr_attachment = all_ocr_attachment_list
    for stamp_img in all_ocr_attachment:
        stamp_re = {'success': 1, 'data': ''}
        if '附件4' in stamp_img[0] or '附件5' in stamp_img[0] or '附件6' in stamp_img[0]:
            continue
        if '附件2' in stamp_img[0] and stamp_result_2 == True:
            continue
        elif '附件3' in stamp_img[0] and stamp_result_3 == True:
            continue
        else:
            stamp_re = Ocr().stamp_identify(stamp_img[1], ['电力', '供电'])
        if '附件1' in stamp_img[0]:
            stamp_count_1 = stamp_count_1 + 1
            if '附件1' in rar_file:
                pass
            else:
                if not file_flag1:
                    auditResult.auditStatus = '审核中'  # 返回稽核结果为转人工
                    audit_list.append(
                        [str(bill_number), '发票印章', '“电力”或“供电”', '', '0', 'RPA', '附件1发票不存在',
                         AffixUtils.get_affix_url_with_name(affix_list_from_db, '附件1'), '5', ''])
                else:
                    if stamp_re['success'] == 1:
                        file_list1.append('不合格')
                        stamp1.append(stamp_re['data'])

                        auditResult.auditStatus = '审核中'  # 返回稽核结果为转人工
                    else:
                        file_list1.append('合格')
                        stamp1.append(stamp_re['data'])

        if '附件2' in stamp_img[0]:
            stamp_count_2 = stamp_count_2 + 1
            if '附件2' in rar_file:
                pass
            else:
                if not file_flag2:
                    auditResult.auditStatus = '审核中'  # 返回稽核结果为转人工
                    audit_list.append(
                        [str(bill_number), '电力明细印章', '“电力”或“供电”', '', '0', 'RPA', '附件2电力明细清单不存在',
                         AffixUtils.get_affix_url_with_name(affix_list_from_db, '附件2'), '5', ''])
                else:
                    if stamp_re['success'] == 1:
                        file_list2.append('不合格')
                        stamp2.append(stamp_re['data'])
                        auditResult.auditStatus = '审核中'  # 返回稽核结果为转人工
                    else:
                        file_list2.append('合格')
                        stamp2.append(stamp_re['data'])
                        stamp_result_2 = True

        if '附件3' in stamp_img[0]:
            stamp_count_3 = stamp_count_3 + 1
            stamp_re = Ocr().stamp_identify(stamp_img[1], ['铁塔', '移动'])
            if '附件3' in rar_file:
                pass
            else:
                if not file_flag3:
                    auditResult.auditStatus = '审核中'  # 返回稽核结果为转人工
                    audit_list.append(
                        [str(bill_number), '分摊比例印章', '“铁塔”“移动”', '', '0', 'RPA', '附件3分摊比例不存在',
                         AffixUtils.get_affix_url_with_name(affix_list_from_db, '附件3'), '5', ''])
                else:
                    if stamp_re['success'] == 1:
                        file_list3.append('不合格')
                        stamp3.append(stamp_re['data'])
                        auditResult.auditStatus = '审核中'  # 返回稽核结果为转人工
                    else:
                        file_list3.append('合格')
                        stamp3.append(stamp_re['data'])
                        stamp_result_3 == True

    if '附件1' in rar_file:
        auditResult.auditStatus = '审核中'  # 返回稽核结果为转人工
        audit_list.append(
            [str(bill_number), '发票印章', '“电力”或“供电”', '', '0', 'RPA', '附件1文件格式不正确',
             AffixUtils.get_affix_url_with_name(affix_list_from_db, '附件1'), '5', ''])
    if '附件2' in rar_file:
        auditResult.auditStatus = '审核中'  # 返回稽核结果为转人工
        audit_list.append(
            [str(bill_number), '电力明细印章', '“电力”或“供电”', '', '0', 'RPA', '附件2文件格式不正确',
             AffixUtils.get_affix_url_with_name(affix_list_from_db, '附件2'), '5', ''])
    if '附件3' in rar_file:
        auditResult.auditStatus = '审核中'  # 返回稽核结果为转人工
        audit_list.append(
            [str(bill_number), '分摊比例印章', '“铁塔”“移动”“联通”“电信”', '', '0', 'RPA', '附件3文件格式不正确',
             AffixUtils.get_affix_url_with_name(affix_list_from_db, '附件3'), '5', ''])
    # 附件1批量或者单站印章识别结果存入队列
    if len(file_list1) >= 2:
        stamp = ''
        for i in stamp1:
            if i == '':
                continue
            stamp = stamp + str(i) + ';'
        if '合格' in file_list1 and '不合格' in file_list1:
            audit_list.append([str(bill_number), '发票印章',
                               '“电力”或“供电”', stamp, '0', 'RPA', '部分合格',
                               AffixUtils.get_affix_url_with_name(affix_list_from_db, '附件1'), '5', ''])
            auditResult.auditStatus = '审核中'  # 返回稽核结果为转人工
        else:
            if '不合格' in file_list1:
                audit_list.append([str(bill_number), '发票印章',
                                   '“电力”或“供电”', stamp, '0', 'RPA', '均不合格',
                                   AffixUtils.get_affix_url_with_name(affix_list_from_db, '附件1'), '5', ''])
                auditResult.auditStatus = '审核中'  # 返回稽核结果为转人工
            else:
                audit_list.append([str(bill_number), '发票印章',
                                   '“电力”或“供电”', stamp, '1', 'RPA', '全部合格',
                                   AffixUtils.get_affix_url_with_name(affix_list_from_db, '附件1'), '5', ''])
    else:
        if '不合格' in file_list1:
            audit_list.append([str(bill_number), '发票印章', '“电力”或“供电”', stamp1[0], '0', 'RPA', '附件1中未检测到指定印章（“电力”或“供电”）',
                               AffixUtils.get_affix_url_with_name(affix_list_from_db, '附件1'), '5', ''])
            auditResult.auditStatus = '审核中'  # 返回稽核结果为转人工
        elif '合格' in file_list1:
            audit_list.append([str(bill_number), '发票印章', '“电力”或“供电”', stamp1[0], '1', 'RPA', '合格',
                               AffixUtils.get_affix_url_with_name(affix_list_from_db, '附件1'), '5', ''])
    # 附件2批量或者单站印章识别结果存入队列
    if len(file_list2) >= 2:
        stamp = ''
        for i in stamp2:
            if i == '':
                continue
            stamp = stamp + str(i) + ';'
        if '合格' in file_list2:
            audit_list.append([str(bill_number), '电力明细印章',
                               '“电力”或“供电”', stamp, '1', 'RPA', '合格',
                               AffixUtils.get_affix_url_with_name(affix_list_from_db, '附件2'), '5', ''])
        else:
            audit_list.append([str(bill_number), '电力明细印章',
                               '“电力”或“供电”', stamp, '0', 'RPA', '不合格',
                               AffixUtils.get_affix_url_with_name(affix_list_from_db, '附件2'), '5', ''])
            auditResult.auditStatus = '审核中'  # 返回稽核结果为转人工
    else:
        if '不合格' in file_list2:
            audit_list.append(
                [str(bill_number), '电力明细印章', '“电力”或“供电”', stamp2[0], '0', 'RPA', '附件2用电明细清单中未检测到指定印章（“电力”或“供电”）',
                 AffixUtils.get_affix_url_with_name(affix_list_from_db, '附件2'), '5', ''])
            auditResult.auditStatus = '审核中'  # 返回稽核结果为转人工
        elif '合格' in file_list2:
            audit_list.append([str(bill_number), '电力明细印章', '“电力”或“供电”', stamp2[0], '1', 'RPA', '合格',
                               AffixUtils.get_affix_url_with_name(affix_list_from_db, '附件2'), '5', ''])
    # 附件3批量或者单站印章识别结果存入队列
    if len(file_list3) >= 2:
        stamp = ''
        for i in stamp3:
            if i == '':
                continue
            stamp = stamp + str(i) + ';'
        if '合格' in file_list3:
            audit_list.append([str(bill_number), '分摊比例印章',
                               '“铁塔”“移动”“联通”“电信”', stamp, '1', 'RPA', '合格',
                               AffixUtils.get_affix_url_with_name(affix_list_from_db, '附件3'), '5', ''])
        else:
            audit_list.append([str(bill_number), '分摊比例印章',
                               '“铁塔”“移动”“联通”“电信”', stamp, '0', 'RPA', '不合格',
                               AffixUtils.get_affix_url_with_name(affix_list_from_db, '附件3'), '5', ''])
            auditResult.auditStatus = '审核中'  # 返回稽核结果为转人工
    else:
        if '不合格' in file_list3:
            audit_list.append([str(bill_number), '分摊比例印章', '“铁塔”“移动”“联通”“电信”', stamp3[0], '0', 'RPA',
                               '附件3用电分割单中未检测到指定印章（“铁塔”“移动”“联通”“电信”）',
                               AffixUtils.get_affix_url_with_name(affix_list_from_db, '附件3'), '5', ''])
            auditResult.auditStatus = '审核中'  # 返回稽核结果为转人工
        elif '合格' in file_list3:
            audit_list.append([str(bill_number), '分摊比例印章', '“铁塔”“移动”“联通”“电信”', stamp3[0], '1', 'RPA', '合格',
                               AffixUtils.get_affix_url_with_name(affix_list_from_db, '附件3'), '5', ''])
    return audit_list,auditResult


"""
    核销单 - 印章识别稽核
"""
def audit_rule_verific_stamp(all_ocr_attachment,rar_file,auditResult,audit_list,bill_number,
                         file_flag1,affix_list_from_db,file_list1,stamp1,file_flag2,
                         file_list2,stamp2):
    # 只做附件3判断是否存在，无论存在或不存在都不做任何动作
    stamp_count_1, stamp_count_2, stamp_count_3 = 0, 0, 0  # 记录各附件调用次数
    stamp_result_2 = False
    for stamp_img in all_ocr_attachment:
        if '附件4' in stamp_img[0] or '附件3' in stamp_img[0] or '附件6' in stamp_img[0]:
            continue
        stamp_re = {'success': 1, 'data': ''}
        # print(stamp_count_1, stamp_count_2, stamp_count_3)
        if '附件2' in stamp_img[0] and stamp_result_2 == True:
            continue
        else:
            stamp_re = Ocr().stamp_identify(stamp_img[1], ['电力', '供电'])
        if '附件1' in stamp_img[0]:
            stamp_count_1 = stamp_count_1 + 1
            if '附件1' in rar_file:
                pass
            else:
                if not file_flag1:
                    audit_list.append(
                        [str(bill_number), '发票印章', '“电力”或“供电”', '', '0', 'RPA', '附件1发票不存在',
                         AffixUtils.get_affix_url_with_name(affix_list_from_db, '附件1'), '5', ''])
                else:
                    if stamp_re['success'] == 1:
                        file_list1.append('不合格')
                        stamp1.append(stamp_re['data'])

                    else:
                        file_list1.append('合格')
                        stamp1.append(stamp_re['data'])

        if '附件2' in stamp_img[0]:
            stamp_count_2 = stamp_count_2 + 1
            if '附件2' in rar_file:
                pass
            else:
                if not file_flag2:
                    audit_list.append(
                        [str(bill_number), '电力明细印章', '“电力”或“供电”', '', '0', 'RPA', '附件2电力明细不存在',
                         AffixUtils.get_affix_url_with_name(affix_list_from_db, '附件2'), '5', ''])
                else:
                    if stamp_re['success'] == 1:
                        file_list2.append('不合格')
                        stamp2.append(stamp_re['data'])

                    else:
                        stamp_result_2 = True
                        file_list2.append('合格')
                        stamp2.append(stamp_re['data'])

    if '附件1' in rar_file:
        audit_list.append(
            [str(bill_number), '发票印章', '“电力”或“供电”', '', '0', 'RPA', '附件1文件格式不正确',
             AffixUtils.get_affix_url_with_name(affix_list_from_db, '附件1'), '5', ''])
    if '附件2' in rar_file:
        audit_list.append(
            [str(bill_number), '电力明细印章', '“电力”或“供电”', '', '0', 'RPA', '附件2文件格式不正确',
             AffixUtils.get_affix_url_with_name(affix_list_from_db, '附件2'), '5', ''])
    if len(file_list1) >= 2:
        stamp = ''
        for i in stamp1:
            if i == '':
                continue
            if str(i) not in stamp:
                stamp = stamp + str(i) + ';'
        if '合格' in file_list1 and '不合格' in file_list1:
            audit_list.append([str(bill_number), '发票印章',
                               '“电力”或“供电”', stamp, '0', 'RPA', '部分合格',
                               AffixUtils.get_affix_url_with_name(affix_list_from_db, '附件1'), '5', ''])
            auditResult.auditStatus = '审核中'  # 返回稽核结果为转人工
        else:
            if '不合格' in file_list1:
                audit_list.append([str(bill_number), '发票印章',
                                   '“电力”或“供电”', stamp, '0', 'RPA', '均不合格',
                                   AffixUtils.get_affix_url_with_name(affix_list_from_db, '附件1'), '5', ''])
                auditResult.auditStatus = '审核中'  # 返回稽核结果为转人工
            else:
                audit_list.append([str(bill_number), '发票印章',
                                   '“电力”或“供电”', stamp, '1', 'RPA', '全部合格',
                                   AffixUtils.get_affix_url_with_name(affix_list_from_db, '附件1'), '5', ''])
    else:
        if '不合格' in file_list1:
            audit_list.append(
                [str(bill_number), '发票印章', '“电力”或“供电”', stamp1[0], '0', 'RPA', '附件1中未检测到指定印章',
                 AffixUtils.get_affix_url_with_name(affix_list_from_db, '附件1'), '5', ''])
            auditResult.auditStatus = '审核中'  # 返回稽核结果为转人工
        elif '合格' in file_list1:
            audit_list.append([str(bill_number), '发票印章',
                               '“电力”或“供电”', stamp1[0], '1', 'RPA', '合格',
                               AffixUtils.get_affix_url_with_name(affix_list_from_db, '附件1'), '5',
                               ''])
    if len(file_list2) >= 2:
        stamp = ''
        for i in stamp2:
            if str(i) not in stamp:
                stamp = stamp + str(i) + ';'
        if '合格' in file_list2:
            audit_list.append([str(bill_number), '电力明细印章',
                               '“电力”或“供电”', stamp, '1', 'RPA', '合格',
                               AffixUtils.get_affix_url_with_name(affix_list_from_db, '附件2'), '5', ''])
        else:
            audit_list.append([str(bill_number), '电力明细印章',
                               '“电力”或“供电”', stamp, '0', 'RPA', '不合格',
                               AffixUtils.get_affix_url_with_name(affix_list_from_db, '附件2'), '5', ''])
            auditResult.auditStatus = '审核中'  # 返回稽核结果为转人工
    else:
        if '不合格' in file_list2:
            audit_list.append(
                [str(bill_number), '电力明细印章', '“电力”或“供电”', stamp2[0],
                 '0', 'RPA', '附件2用电明细清单中未检测到指定印章（“电力”或“供电”）',
                 AffixUtils.get_affix_url_with_name(affix_list_from_db, '附件2'),
                 '5', ''])
            auditResult.auditStatus = '审核中'  # 返回稽核结果为转人工
        elif '合格' in file_list2:
            audit_list.append([str(bill_number), '电力明细印章',
                               '“电力”或“供电”', stamp2[0], '1', 'RPA', '合格',
                               AffixUtils.get_affix_url_with_name(affix_list_from_db, '附件2'), '5',
                               ''])
    return audit_list,auditResult